﻿using System;
using System.Threading.Tasks;

class AsyncPowerCalculator
{
    // 异步计算一个数的n次幂
    static async Task<double> CalculatePowerAsync(double num, int exponent)
    {
        // 使用Task.Run将计算放入线程池，避免阻塞主线程
        return await Task.Run(() =>
        {
            double result = 1;
            for (int i = 0; i < exponent; i++)
            {
                result *= num;
            }
            return result;
        });
    }

    // 异步计算a^58 + b^50
    static async Task<double> CalculateExpressionAsync(double a, double b)
    {
        // 并行执行两个幂运算（异步非阻塞）
        Task<double> powerA = CalculatePowerAsync(a, 58);
        Task<double> powerB = CalculatePowerAsync(b, 50);

        // 等待两个计算都完成后求和
        await Task.WhenAll(powerA, powerB);
        return powerA.Result + powerB.Result;
    }

    static async Task Main(string[] args)
    {
        try
        {
            // 输入a和b（确保在1-10之间）
            Console.Write("请输入1-10之间的实数a: ");
            double a = double.Parse(Console.ReadLine() ?? "0");
            Console.Write("请输入1-10之间的实数b: ");
            double b = double.Parse(Console.ReadLine() ?? "0");

            if (a <= 1 || a >= 10 || b <= 1 || b >= 10)
            {
                Console.WriteLine("a和b必须是大于1且小于10的实数");
                return;
            }

            // 异步计算并输出结果
            Console.WriteLine("正在计算...");
            double result = await CalculateExpressionAsync(a, b);
            Console.WriteLine($"计算结果: {a}^58 + {b}^50 = {result:F6}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"错误: {ex.Message}");
        }
    }
}